<?php

namespace app\Api\controller;

use app\common\controller\Api;
use app\common\model\Liquor as liquorModel;
use think\Db;

class Liquor extends Api
{

    /**
     * 存酒列表
     *
     * @return \think\Response
     */
    public function liquorList()
    {
        $liquor_model = new liquorModel();
        $page = $this->request->get('page', 1);
        $size = $this->request->get('size', 20);
        $size = 2000;
        $condition = [
            ['user_id', 'eq', $this->userId],
            ['status', 'eq', $this->request->get('status', 'deposit')]    
        ];
        $this->result['data'] = $liquor_model::with('goods')->where($condition)->limit(($page-1)*$size, $size)->order('id', 'desc')->select();
        $this->result['data'] = obj_to_array($this->result['data']);
        foreach ($this->result['data'] as &$info) {
            $info['ctime'] = date('Y-m-d H:i:s', $info['ctime']);
            $info['expire_time'] = date('Y-m-d', $info['expire_time']);
            $info['goods']['image_url'] = _sImage($info['goods']['image_id']);
        }
        return $this->result;
    }
    
    public function getLiquor()
    {
        $id = $this->request->post('id');
        $numer = $this->request->post('number');
        $room_bn = $this->request->post('room_bn');
        if(empty($id) || empty($numer) || empty($room_bn)) {
            $this->error['msg'] = '缺少必要参数';
            return $this->error;
        }
        //房间不消费无法取酒
        $hour=date("G");
        if($hour < 3){
            $time = \think\helper\Time::yesterday();
        }else{
            $time = \think\helper\Time::today();
        }
        $order_condition = [
            ['user_id' , '=', $this->userId],
            ['room_bn', '=', $room_bn],
            ['status', '=', 2],
            ['ctime', 'between', $time] 
        ];
        $list = model('Order')->where($order_condition)->select();
        if(empty($list)) {
            $this->error['msg'] = '至少消费一次,才能取酒';
            return $this->error;
        }
        
        $liquor_model = new liquorModel();
        $condition = [
            ['id', 'eq', $id],
            ['user_id', 'eq', $this->userId]
        ];
        $liquor_info = $liquor_model->where($condition)->find();
        if(!$liquor_info) {
            $this->error['msg'] = '无此存酒记录';
            return $this->error;
        }
        if($liquor_info->number < $numer) {
            $this->error['msg'] = '超出存酒数量';
            return $this->error;
        }
        Db::startTrans();
        $liquor_order_model = new \app\common\model\LiquorOrder();
        $liquor_order_model->order_bn = build_order_no();
        $liquor_order_model->number = $numer;
        $liquor_order_model->user_id = $this->userId;
        $liquor_order_model->liquor_id = $liquor_info->id;
        $liquor_order_model->room_bn = $room_bn;
        if($liquor_order_model->save()) {
            $update = [];
            $update['number'] = $liquor_info->number-$numer;
            if($liquor_info->number-$numer == 0) {
                $update['status'] = 'took';
            }
            if($liquor_model->where('id', $id)->update($update)) {
                Db::commit();
                doRequest(config('jshop.jpush_host'), ['id' => $liquor_order_model->id, 'method' => 'pay_bill']);
                return $this->result;
            } else {
                Db::rollback();
                return $this->error;
            }
        } else {
            return $this->error;
        }
    }
}
